package hms.payment;

import hms.ReservationsModule.CheckInView;
import hms.database.*;

import java.sql.*;
import java.util.regex.Pattern;
import java.util.Vector;
import java.awt.event.*;
import javax.swing.*;


/*This class sets up all payment options for guest to choose from when selecting payment option.
 * Includes functionality for the following options:
 *  -Cash
 *  -Check
 *  -Debit
 *  -Credit
 * 
 * @author Edgar Onukwugha*/

public class PaymentOption extends CheckInView{
	
	//defines all components for PaymentOption class
	//labels for each element
	JLabel cash = new JLabel("Cash"), 
			cardNumberLabel = new JLabel("Card Number"),
			CVV2Label = new JLabel("CVV2"),
			MonthLabel = new JLabel("Month"),
			YrLabel = new JLabel("Year"), 
			cardOwnerLabel = new JLabel("Card Owner"),
			acctNumLabel = new JLabel("Accout Number"),
			routingNumLabel = new JLabel("Routing Number");
	
	//elements
	JTextField currencyAmtEntry;
	JTextField cardNumber, CVV2, expireMon, expireYr, cardOwner;
	JComboBox cardCorp;
	JTextField acctNumber, routingNumber;
	
	//choices for financial services corporation
	String[] cardCoName = {
			"Select Card",
			"VISA",
			"MasterCard",
			"Discover",
			"American Express", 
			"Diner's Club"};
	
	//used to parse together expiration date for credit/debit card
	String expMonth, expYr, expDate;
	
	DAO db = new DAO("HMSDatabase", "db_files\\", true);
	
	//sets all events for Cash option
	public JPanel cashOption(){
		JPanel cashPanel = new JPanel();
		cashPanel.setLayout(new BoxLayout(cashPanel, BoxLayout.X_AXIS));
		
		//initializes all components for cash option
		currencyAmtEntry = new JTextField();
		
		cashPanel.add(cash);
		cashPanel.add(currencyAmtEntry);
		
		return cashPanel;
	}
	
	//sets all events for Check option
	public JPanel checkOption(){
		//initializes all components for Check option
		acctNumber = new JTextField();
		routingNumber = new JTextField();
		
		//add to panel
		JPanel checkPanel = new JPanel();
		checkPanel.setLayout(new BoxLayout(checkPanel, BoxLayout.Y_AXIS));
		
		checkPanel.add(acctNumLabel);
		checkPanel.add(acctNumber);
		checkPanel.add(routingNumLabel);
		checkPanel.add(routingNumber);
		
		//TODO: add border for checkOption
		
		return checkPanel;
	}

	//sets all events for Credit card option
	public JPanel creditOption(){
		//initializes all components for Credit option
		cardNumber = new JTextField();
		CVV2 = new JTextField(3); 
		expireMon = new JTextField(2);
		expireYr = new JTextField(4);
		cardOwner = new JTextField();
		cardCorp = new JComboBox(cardCoName);
		
		//initializes expiration date String
		expMonth = expireMon.getText();
		expYr = expireYr.getText();
		expDate = expMonth + "/" + expYr;
		
		cardCorp.addActionListener(new ActionListener(){
	          public void actionPerformed(ActionEvent event){
	        	  /*information used to compare card numbers with proper format*/
	        	  //use to check with string from textfield
	        	  Pattern phoneNumberResult = Pattern.compile("(\\d{3})\\s\\d{3}-\\d{4}");
	        	  Pattern cardNumberResult = Pattern.compile("\\d{13,16}");
	        	  
	        	  //used to store string from textfield for parsing
	        	  Vector<String> cardResult = new Vector<String>();
	        	  //gets text from cardNumber textfield
	        	  String cardNumberInput = cardNumber.getText();
	        	  
	        	  /*set card number, CVV2 format*/
	        	  //TODO: set all formats for card numbers, CVV2 codes
	        	  if(event.getSource() == "VISA"){
	        		  
	        		  //adds input from textfield into vector for comparison
	        		  cardResult.add(cardNumberInput);
	        		  //checks if input card number is equal to VISA's format
	        		  cardNumberResult = Pattern.compile("4\\d{15}");
	        		  //if (cardNumberInput == cardNumberResult){}
	        		  //TODO: check for '4' as MII
	        		  /*has card length of 16*/
	        		//TODO: check for length of card to see if it runs over
	        	  }
	        	  
	        	  if(event.getSource() == "MasterCard"){
	        		  
	        		  //checks if input card number is equal to MasterCard's format
	        		  cardNumberResult = Pattern.compile("5[1-5]\\d{14}");
	        		  //check for '51', '52', '53', '54', or '55' as MII
	        		  /*has card length of 16*/
	        		  //TODO: check for length of card to see if it runs over
	        	  }
	        	  
	        	  if(event.getSource() == "Discover"){
	        		  
	        		  //checks if input card number is equal to Discover's format
	        		  cardNumberResult = Pattern.compile("601\\d{13}");
	        		  //checks for '6011xx' as MII
	        		  /*has card length of 16*/
	        		//TODO: check for length of card to see if it runs over
	        	  }
	        	  
	        	  if(event.getSource() == "American Express"){
	        		  
	        		  //checks if input card number is equal to American Express' format
	        		  cardNumberResult = Pattern.compile("3[4,7]\\d{13}");
	        		  //TODO: check for '34xxx' or '37xxx' as MII
	        		  /*has card length of 15*/
	        		//TODO: check for length of card to see if it runs over
	        	  }
	        	  
	        	  if(event.getSource() == "Diner's Club"){
	        		  //checks if input card number is equal to Diner's Club's format
	        		  cardNumberResult = Pattern.compile("30[0-5]\\d{11}");
	        		  //TODO: check for range from '300xxx' to '305xxx'
	        		  /*has card length of 14*/
	        		//TODO: check for length of card to see if it runs over
	        	  }
	          }
	      });
		
		//add to panel
		JPanel creditPanel = new JPanel();
		creditPanel.setLayout(new BoxLayout(creditPanel, BoxLayout.Y_AXIS));
		
		creditPanel.add(cardNumberLabel);
		creditPanel.add(cardNumber);
		creditPanel.add(CVV2Label);
		creditPanel.add(CVV2);
		creditPanel.add(MonthLabel);
		creditPanel.add(expireMon);
		creditPanel.add(YrLabel);
		creditPanel.add(expireYr);
		creditPanel.add(cardOwnerLabel);
		creditPanel.add(cardOwner);
		creditPanel.add(cardCorp);
		
		return creditPanel;
	}
	
	//sets all events for Debit card option
	public JPanel debitOption(){
		//initializes all components for Debit option
		cardNumber = new JTextField();
		CVV2 = new JTextField(); 
		expireMon = new JTextField();
		expireYr = new JTextField();
		cardOwner = new JTextField();
		cardCorp = new JComboBox(cardCoName);
		
		
		//initializes expiration date String
		expMonth = expireMon.getText();
		expYr = expireYr.getText();
		expDate = expMonth + "/" + expYr;
		
		cardCorp.addActionListener(new ActionListener(){
	          public void actionPerformed(ActionEvent event){
	        	  /*information used to compare card numbers with proper format*/
	        	  //use to check with string from textfield
	        	  Pattern phoneNumberResult = Pattern.compile("(\\d{3})\\s\\d{3}-\\d{4}");
	        	  Pattern cardNumberResult = Pattern.compile("\\d{13,16}");
	        	  
	        	  //used to store string from textfield for parsing
	        	  Vector<String> cardResult = new Vector<String>();
	        	  //gets text from cardNumber textfield
	        	  String cardNumberInput = cardNumber.getText();
	        	  
	        	  /*set card number, CVV2 format*/
	        	  //TODO: set all formats for card numbers, CVV2 codes
	        	  if(event.getSource() == "VISA"){
	        		  
	        		  //adds input from textfield into vector for comparison
	        		  cardResult.add(cardNumberInput);
	        		  //checks if input card number is equal to VISA's format
	        		  cardNumberResult = Pattern.compile("4\\d{15}");
	        		  //if (cardNumberInput == cardNumberResult){}
	        		  //TODO: check for '4' as MII
	        		  /*has card length of 16*/
	        		//TODO: check for length of card to see if it runs over
	        	  }
	        	  
	        	  if(event.getSource() == "MasterCard"){
	        		  
	        		  //checks if input card number is equal to MasterCard's format
	        		  cardNumberResult = Pattern.compile("5[1-5]\\d{14}");
	        		  //check for '51', '52', '53', '54', or '55' as MII
	        		  /*has card length of 16*/
	        		  //TODO: check for length of card to see if it runs over
	        	  }
	        	  
	        	  if(event.getSource() == "Discover"){
	        		  
	        		  //checks if input card number is equal to Discover's format
	        		  cardNumberResult = Pattern.compile("601\\d{13}");
	        		  //checks for '6011xx' as MII
	        		  /*has card length of 16*/
	        		//TODO: check for length of card to see if it runs over
	        	  }
	        	  
	        	  if(event.getSource() == "American Express"){
	        		  
	        		  //checks if input card number is equal to American Express' format
	        		  cardNumberResult = Pattern.compile("3[4,7]\\d{13}");
	        		  //TODO: check for '34xxx' or '37xxx' as MII
	        		  /*has card length of 15*/
	        		//TODO: check for length of card to see if it runs over
	        	  }
	        	  
	        	  if(event.getSource() == "Diner's Club"){
	        		  //checks if input card number is equal to Diner's Club's format
	        		  cardNumberResult = Pattern.compile("30[0-5]\\d{11}");
	        		  //TODO: check for range from '300xxx' to '305xxx'
	        		  /*has card length of 14*/
	        		//TODO: check for length of card to see if it runs over
	        	  }
	          }
	      });
		
		//add to panel
		JPanel debitPanel = new JPanel();
		debitPanel.setLayout(new BoxLayout(debitPanel, BoxLayout.Y_AXIS));
		
		debitPanel.add(cardNumberLabel);
		debitPanel.add(cardNumber);
		debitPanel.add(CVV2Label);
		debitPanel.add(CVV2);
		debitPanel.add(MonthLabel);
		debitPanel.add(expireMon);
		debitPanel.add(YrLabel);
		debitPanel.add(expireYr);
		debitPanel.add(cardOwnerLabel);
		debitPanel.add(cardOwner);
		debitPanel.add(cardCorp);
		
		return debitPanel;
	}	
}
